import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';

class GetStoragePage extends GetView {

  @override
  Widget build(BuildContext context) {
    final box = GetStorage('user');
    // 监听整个容器
    box.listen((){
      print('box-user changed');
    });
    // 监听某个 key
    box.listenKey('username', (value){
      print('new username is $value');
    });
    var username = box.read('username');

    return Scaffold(
      appBar: AppBar(title: Text('GetStoragePage')),

      body: Container(
        margin: EdgeInsets.all(20),
        child: ListView(
          children: [
            Text(
              username.toString(),
              style: Theme.of(context).textTheme.headline5
            ),
            ElevatedButton(
              onPressed: () async {
                await box.write('username', '初六');
              }, 
              child: Text('写入')
            ),
            ElevatedButton(
              onPressed: () {
                var u = box.read('username');
                Get.snackbar('用户名', u.toString());
              }, 
              child: Text('读取')
            ),
            ElevatedButton(
              onPressed: () {
                var u = box.remove('username');
                Get.snackbar('用户名', box.read('username').toString());
              }, 
              child: Text('删除')
            ),
            ElevatedButton(
              onPressed: () {
                print(box.hasData('username'));
              }, 
              child: Text('hasData')
            ),
            ElevatedButton(
              onPressed: () {
                print(box.getKeys());
              }, 
              child: Text('getKeys')
            ),
            ElevatedButton(
              onPressed: () {
                print(box.getValues());
              }, 
              child: Text('getValues')
            ),
            ElevatedButton(
              onPressed: () {
                box.erase();
                print(box.getKeys());
              }, 
              child: Text('清空')
            ),
            ElevatedButton(
              onPressed: () {
                box.write('token', '123456789');
              }, 
              child: Text('写入 token')
            ),
          ],
        )
      )
    );
  }
}